﻿using Microsoft.Extensions.Logging;
using Warehouse.Services.Repository;
using WCS.Entities;

namespace WCS.Services.CommandService.Stacker
{
    [Command(RobotStates = RobotStatus.Delivery, TaskStates = TaskState.Docking, IsCheckedDistance = false)]
    public class StackerArrivedCommand : BaseCommand
    {
        public StackerArrivedCommand(ILogger<BaseCommand> logger, Repository<Job> basicTaskRepository) : base(logger, basicTaskRepository)
        {

        }

        protected override Task<bool> Dock(TransferPort port, CommandAttribute attribute)
        {
            //写入AGV值
            if (port.BelongDevice.Operation.ReadInt16(port.PlcAddress).Value == 2)
            {
                return Task.FromResult(true);
            }
            else
            {

                var result = port.BelongDevice.Operation.Write(port.AgvAddress, (short)2);

                if (result.IsSucceed)
                {
                    Thread.Sleep(1000);
                    return Task.FromResult(port.BelongDevice.Operation.ReadInt16(port.PlcAddress).Value == 2);
                }
                logger.LogWarning("Robot{0} excute {1} notice target-{1} failed!", null, "StackerArrvied", port.Name);
                return Task.FromResult(false);


            }
        }


    }
}
